import React from 'react';
import { message } from 'antd';
import { Row, Col, Form, Modal, Input, Select, Radio, Switch, Button, Upload, Icon, Table } from 'antd';
import MapInputs from '../../../components/MapInputs';
import { DEFAULT_PAGE_SIZE } from '../../../utils/constants';
import { addKeyToDataSource } from '../../../utils';
import _ from 'lodash';
import styles from '../../common.css';
const FormItem = Form.Item;
const TextArea = Input.TextArea;
const Option = Select.Option;
const RadioGroup = Radio.Group;
const formItemLayout = {
  labelCol: {
    span: 6,
  },
  wrapperCol: {
    span: 17,
  },
};
const formItemLayout1 = {
  labelCol: {
    span: 3,
  },
  wrapperCol: {
    span: 20,
  },
};

@Form.create()
export default class ProjectModal extends React.Component {
  constructor(props) {
    super(props);
  }
  
  handleOk = () => { // 点击OK时，执行修改or新增
    const { dispatch, currentUser, form: { validateFields, getFieldsValue } } = this.props;
    validateFields((errors) => {
      if (errors) {
        return;
      }
      let adddata={
        // ...currentUser,
        ...getFieldsValue(),
      };
      let editdata={
        ...currentUser,
        ...getFieldsValue(),
      };
      const data = currentUser.id? editdata : adddata;
      // data.test_case_path =JSON.stringify(data.test_case_path);
      // data.paramobj = JSON.stringify(data.paramobj);
      const type = `project/${currentUser.id ? 'updateProject' : 'addProject'}`;// 根据是否有id
      console.log("type",type)
      dispatch({
        type,
        payload: {
          param: data,
          cb: this.handleCancel,
        },
      });


    });
  };
  handleCancel = () => {
    const { dispatch, form: { resetFields }, onCancel } = this.props;
    resetFields();
    onCancel();
    // dispatch({
    //   type: 'testreport/save',
    //   payload: {
    //     RelatedJira: [],
    //   },
    // });
  };
  handleChange = (value) =>{
    const { dispatch ,jira_project_info,projectlist, form: { validateFields, getFieldsValue } } = this.props;
    let tempdict={}
    if (projectlist.length!==0){
      projectlist.map((item,index)=>{
        // console.log("item",item.english_name)
        let tempvalue=item.name
        if (tempvalue===value)
        {
          tempdict.project_id=item.project_id
          tempdict.english_name=item.english_name
          tempdict.board_id=item.board_id
        }
      })
    }
    
    const {form} = this.props;
    form.setFieldsValue({english_name: tempdict.english_name});
    form.setFieldsValue({project_id: tempdict.project_id});
    form.setFieldsValue({board_id: tempdict.board_id});
  }
  render() {

    const { loading, visible, currentUser, ButtonVisible,projectinfo, projectlist, form: { getFieldDecorator } } = this.props;
    // console.log("jira_project_info",jira_project_info)
    const modalProps = {
      title: currentUser.project_id ? '修改' : '新增', // 根据主键判断是否存在数据来决定走修改还是编辑，新增情况下，不会存在id，因为currentUser为空
      visible,
      // width: 780, //默认520
      wrapClassName: 'vertical-center-modal',
      confirmLoading: loading,
      maskClosable: false,
    };
    return (
      <div>
        <Form>
          <Row gutter={24}>
            <Col span={12}>
              <FormItem
                label="项目名称"
                {...formItemLayout}
              >
                {getFieldDecorator('name', {
                  initialValue: currentUser.name || '',
                  rules: [
                    {
                      required: true,
                      message: '请输入项目名称',
                    },
                  ],
                })(
                  <Select
                    showSearch
                    optionFilterProp="children"
                    onChange={this.handleChange}
                    filterOption={(input, option) =>
                      option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                  >
                    <Option key={-1} value="">请选择项目名称</Option>
                    {
                      projectlist.map((item, index) => {
                        return (<Option key={index} value={item.name}>{item.name}</Option>);
                      })
                    }
                  </Select>
                )}
              </FormItem>
            </Col>
            <Col span={12}>
              <FormItem
                label="项目JIRA-KEY"
                {...formItemLayout}
              >
                {getFieldDecorator('english_name', {
                  initialValue: currentUser.english_name||'',
                  rules: [
                    {
                      required: true,
                      message: '请输入项目JIRA KEY值',
                    },
                  ],
                })(
                  <Input/>
                )}
              </FormItem>
            </Col>
            
          </Row>
          <Row gutter={24}>
          <Col span={12}>
              <FormItem
                label="项目JIRA-ID"
                {...formItemLayout}
              >
                {getFieldDecorator('project_id', {
                  initialValue: currentUser.project_id || '',
                  rules: [
                    {
                      required: true,
                      message: '请输入项目JIRA ID',
                    },
                  ],
                })(
                  <Input/>
                )}
              </FormItem>
            </Col>
            <Col span={12}>
              <FormItem
                label="项目看板ID"
                {...formItemLayout}
              >
                {getFieldDecorator('board_id', { // 此处写的参数需与接口参数key对应
                  initialValue: currentUser.board_id || '',
                })(
                  <Input/>
                )}
              </FormItem>
            </Col>
          </Row>
          <Row gutter={24}>
          <Col span={12}>
              <FormItem
                label="测试报告接收邮箱"
                {...formItemLayout}
              >
                {getFieldDecorator('send_testreport_to', { // 此处写的参数需与接口参数key对应
                  initialValue: currentUser.send_testreport_to || '',
                })(
                  <Input placeholder="多个邮箱以英文逗号分隔,如lyl@bdfint.com,zhangsan@bdfint.com"/>
                )}
              </FormItem>
            </Col>
            <Col span={12}>
              <FormItem
                label="冒烟报告接收邮箱"
                {...formItemLayout}
              >
                {getFieldDecorator('send_smokereport_to', { // 此处写的参数需与接口参数key对应
                  initialValue: currentUser.send_smokereport_to || '',
                })(
                  <Input placeholder="多个邮箱以英文逗号分隔,如lyl@bdfint.com,zhangsan@bdfint.com"/>
                )}
              </FormItem>
            </Col>
          </Row>
          <Row gutter={24}>
          <Col span={12}>
              <FormItem
                label="质量报告接收邮箱"
                {...formItemLayout}
              >
                {getFieldDecorator('send_qualityreport_to', { // 此处写的参数需与接口参数key对应
                  initialValue: currentUser.send_qualityreport_to || '',
                })(
                  <Input placeholder="多个邮箱以英文逗号分隔,如lyl@bdfint.com,zhangsan@bdfint.com"/>
                )}
              </FormItem>
            </Col>
            <Col span={12}>
              <FormItem
                label="项目成员"
                {...formItemLayout}
              >
                {getFieldDecorator('project_member', { // 此处写的参数需与接口参数key对应
                  initialValue: currentUser.project_member || '',
                })(
                  <Input/>
                )}
              </FormItem>
            </Col>
          </Row>
          <Row gutter={24} style={{ marginBottom: 8 }}>
            <div className={styles.btnGroup} >
              <Button
                type="primary"
                // loading={loading}
                onClick={this.handleOk}
              >
                保存
              </Button>
              <Button type="primary" style={{ margin: 8 }} onClick={this.handleCancel}>取消</Button>
            </div>
          </Row>
        </Form>
      </div>
    );
  }
}

